﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BaseCardTypes = Wormhole.Engine.CardLibrary.BaseCardTypes;

namespace Wormhole.UI.Application.Controls.Cards
{
	/// <author>Rodney McKay</author>
	/// <date>08 April 2008</date>
	/// <summary>
	/// Render an event card in the play.
	/// </summary>
	public class EventCard : Card
	{
		#region Delegates
		/// <summary>
		/// The delegate for the event that fires when a card should be removed from the user interface.
		/// </summary>
		/// <param name="eventCard">The card that should be removed.</param>
		public delegate void RemoveCardDelegate(EventCard eventCard);
		#endregion

		#region Events
		/// <summary>
		/// This event fires when a card should be removed 
		/// </summary>
		public event RemoveCardDelegate RemoveCard;
		#endregion

		#region Constants
		/// <summary>
		/// The time to elapse before the event card is removed from display.
		/// </summary>
		private const int REMOVE_TIME = 7 * 1000;
		#endregion

		#region User Interface Elements
		/// <summary>
		/// The timer that will be used to remove the event card from the user interface.
		/// </summary>
		private System.Windows.Forms.Timer removeTimer;
		#endregion

		#region Constructor
		/// <summary>
		/// Initialize a default instance of the class.
		/// </summary>
		public EventCard() : base()
		{
		}

		/// <summary>
		/// Create a new instance of the control to render the specific card.
		/// </summary>
		/// <param name="card">The engine card that should be rendered.</param>
		public EventCard(BaseCardTypes.Card card)
			: base(card)
		{
			this.removeTimer = new System.Windows.Forms.Timer();
			this.removeTimer.Interval = REMOVE_TIME;
			this.removeTimer.Tick += new EventHandler(removeTimer_Tick);
			this.removeTimer.Start();
		}
		#endregion

		#region Set Size
		/// <summary>
		/// Set the card size.
		/// </summary>
		protected override void SetSize()
		{
			this.cardImage.Width = 131 * 0.7;
			this.cardImage.Height = 121 * 0.7;
			this.cardImage.Stretch = Stretch.Fill;

			this.outsideBorder.Width = this.cardImage.Width + 10;
			this.outsideBorder.Height = this.cardImage.Height + 10;

			this.Width = this.outsideBorder.Width;
			this.Height = this.outsideBorder.Height;
		}
		#endregion

		#region Event Handlers

		#region Remove Timer
		/// <summary>
		/// The timer tick in which the card should be removed.
		/// </summary>
		private void removeTimer_Tick(object sender, EventArgs e)
		{
			this.removeTimer.Stop();
			this.OnRemoveCard(this);
		}
		#endregion

		#endregion

		#region On Remove Card
		/// <summary>
		/// Fire the remove card event.
		/// </summary>
		/// <param name="eventCard">The card to fire the event for.</param>
		private void OnRemoveCard(EventCard eventCard)
		{
			if (this.RemoveCard != null)
				this.RemoveCard(eventCard);
		}
		#endregion
	}
}
